/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.container.rest;

import com.alien.admin.annotation.Log;
import com.alien.admin.modules.container.domain.K8sDeploymentStatus;
import com.alien.admin.modules.container.service.K8sDeploymentStatusService;
import com.alien.admin.modules.container.service.K8sProjectService;
import com.alien.admin.modules.container.service.dto.K8sDeploymentStatusQueryCriteria;
import com.alien.admin.modules.container.service.dto.K8sProjectQueryCriteria;
import com.alien.admin.modules.basic.domain.Pod;
import com.alien.admin.modules.basic.domain.ReplicaSet;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author zhouseyi
* @date 2021-10-19
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "部署状态管理")
@RequestMapping("/api/k8sDeploymentStatus")
public class K8sDeploymentStatusController {

    private final K8sDeploymentStatusService k8sDeploymentStatusService;
    private final K8sProjectService k8sProjectService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('k8sDeploymentStatus:list')")
    public void download(HttpServletResponse response, K8sDeploymentStatusQueryCriteria criteria) throws IOException {
        k8sDeploymentStatusService.download(k8sDeploymentStatusService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询部署状态")
    @ApiOperation("查询部署状态")
    @PreAuthorize("@el.check('k8sDeploymentStatus:list')")
    public ResponseEntity<Object> query(K8sDeploymentStatusQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(k8sDeploymentStatusService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增部署状态")
    @ApiOperation("新增部署状态")
    @PreAuthorize("@el.check('k8sDeploymentStatus:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody K8sDeploymentStatus resources){
        return new ResponseEntity<>(k8sDeploymentStatusService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改部署状态")
    @ApiOperation("修改部署状态")
    @PreAuthorize("@el.check('k8sDeploymentStatus:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody K8sDeploymentStatus resources){
        k8sDeploymentStatusService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除部署状态")
    @ApiOperation("删除部署状态")
    @PreAuthorize("@el.check('k8sDeploymentStatus:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        k8sDeploymentStatusService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/queryReplicaSetList")
    @Log("查询服务副本列表")
    @ApiOperation("查询服务副本列表")
    @PreAuthorize("@el.check('k8sDeploymentStatus:list')")
    public ResponseEntity<Object> queryReplicaSetList(@Validated @RequestBody K8sProjectQueryCriteria criteria) {
        List<ReplicaSet> replicaSetList = k8sProjectService.queryReplicaSetList(criteria);
        return new ResponseEntity<>(replicaSetList, HttpStatus.OK);
    }

    @PostMapping(value = "/queryPodList")
    @Log("查询服务副本列表")
    @ApiOperation("查询服务副本列表")
    @PreAuthorize("@el.check('k8sDeploymentStatus:list')")
    public ResponseEntity<Object> queryPodList(@Validated @RequestBody K8sProjectQueryCriteria criteria) {
        List<Pod> podList = k8sProjectService.queryPodList(criteria);
        return new ResponseEntity<>(podList, HttpStatus.OK);
    }

    @PostMapping("/queryPodLog")
    @Log("查询pod日志")
    @ApiOperation("查询pod日志")
    @PreAuthorize("@el.check('k8sDeploymentStatus:list')")
    public ResponseEntity<Object> queryPodLog(@Validated @RequestBody K8sProjectQueryCriteria criteria){
        String data = k8sProjectService.queryPodLog(criteria);
        return new ResponseEntity<>(data,HttpStatus.OK);
    }

    @PostMapping("/queryProjectRunningStatus")
    @Log("查询服务运行状态")
    @ApiOperation("查询服务运行状态")
    @PreAuthorize("@el.check('k8sDeploymentStatus:list')")
    public ResponseEntity<Object> queryProjectRunningStatus(@Validated @RequestBody K8sProjectQueryCriteria criteria){
        Integer data = k8sDeploymentStatusService.findProjectRunningStatus(criteria);
        return new ResponseEntity<>(data,HttpStatus.OK);
    }
}
